package com.ruoyi.web.controller.api.money;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.SystemType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.data.domain.DataMoney;
import com.ruoyi.data.service.IDataMoneyService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zzp
 * @version 1.0.0
 * @ClassName IndexController.java
 * @Description 消费接口
 * @createTime 2022年10月09日 16:05:00
 */
@Api("获取消费信息接口")
@RestController
@RequestMapping("/api/money")
public class MoneyController extends BaseController {

    @Autowired
    private IDataMoneyService moneyService;

    @Autowired
    private ISysUserService userService;

    @ApiOperation("获取消费记录")
    @GetMapping("/moneyList")
    public TableDataInfo moneyList(DataMoney money) {
        startPage();
        Long userId;
        if (StringUtils.isNotNull(money.getUserId())) {
            userId = money.getUserId();
        } else {
            userId = getUserId();
        }
        List<DataMoney> list = moneyService.selectDataMoneyListByUserId(userId, money.getMoneyType());
        return getDataTable(list);
    }

    /**
     * 新增消费记录
     */
    @ApiOperation("新增消费记录")
    @Log(title = "消费记录", businessType = BusinessType.INSERT)
    @PostMapping("/addUserMoney")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addUserMoney(@RequestBody DataMoney dataMoney) {
        if (StringUtils.isNull(dataMoney.getUserId())) {
            return AjaxResult.error("请选择用户");
        } else {
            SysUser user = userService.selectUserById(dataMoney.getUserId());
            if (StringUtils.isNull(user)) {
                return AjaxResult.error("用户不存在");
            }
            if (dataMoney.getMoneyType().equals(SystemType.IN.getCode())) {
                user.setMoneyAvailable(user.getMoneyAvailable() + dataMoney.getMoney());
            } else {
                if (user.getMoneyAvailable() < dataMoney.getMoney()) {
                    return AjaxResult.error("用户可用金额不足");
                }
                user.setMoneyAvailable(user.getMoneyAvailable() - dataMoney.getMoney());
            }
            userService.updateUserProfile(user);
            dataMoney.setDeptId(user.getDeptId());
            dataMoney.setNickName(user.getNickName());
            dataMoney.setMoneyCurrent(user.getMoneyAvailable());
            dataMoney.setCreateBy(getUsername());
            moneyService.insertDataMoney(dataMoney);
            return AjaxResult.success();
        }
    }
}
